home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / MPW / indent 1.8 / indent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-19  |  50.9 KB  |  1,840 lines  |  [TEXT/MPS ]

  1. /* Copyright (c) 1992, Free Software Foundation, Inc.  All rights reserved.
  2.  
  3.    Copyright (c) 1985 Sun Microsystems, Inc. Copyright (c) 1980 The Regents
  4.    of the University of California. Copyright (c) 1976 Board of Trustees of
  5.    the University of Illinois. All rights reserved.
  6.  
  7.    Redistribution and use in source and binary forms are permitted
  8.    provided that
  9.    the above copyright notice and this paragraph are duplicated in all such
  10.    forms and that any documentation, advertising materials, and other
  11.    materials related to such distribution and use acknowledge that the
  12.    software was developed by the University of California, Berkeley, the
  13.    University of Illinois, Urbana, and Sun Microsystems, Inc.  The name of
  14.    either University or Sun Microsystems may not be used to endorse or
  15.    promote products derived from this software without specific prior written
  16.    permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  17.    IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
  18.    OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
  19.  
  20. #include "sys.h"
  21. #include "indent.h"
  22. #include <ctype.h>
  23.  
  24. void
  25. usage ()
  26. {
  27. #ifdef macintosh
  28.   fprintf (stderr, "# Usage - Indent file [-o outfile] [ options ]\n");
  29.   fprintf (stderr, "#         Indent file1 file2 [file…] [ options ]\n");
  30. #else
  31.   fprintf (stderr, "usage: indent file [-o outfile ] [ options ]\n");
  32.   fprintf (stderr, "       indent file1 file2 ... fileN [ options ]\n");
  33. #endif
  34.   exit (1);
  35. }
  36.  
  37.  
  38. /* Stuff that needs to be shared with the rest of indent.
  39.    Documented in indent.h.  */
  40. char *labbuf;
  41. char *s_lab;
  42. char *e_lab;
  43. char *l_lab;
  44. char *codebuf;
  45. char *s_code;
  46. char *e_code;
  47. char *l_code;
  48. char *combuf;
  49. char *s_com;
  50. char *e_com;
  51. char *l_com;
  52. struct buf save_com;
  53. char *bp_save;
  54. char *be_save;
  55. int code_lines;
  56. int line_no;
  57. struct fstate keywordf;
  58. struct fstate stringf;
  59. struct fstate boxcomf;
  60. struct fstate blkcomf;
  61. struct fstate scomf;
  62. struct fstate bodyf;
  63. int break_comma;
  64.  
  65. /* Insure that BUFSTRUC has at least REQ more chars left, if not extend it.
  66.       Note:  This may change bufstruc.ptr.  */
  67. #define need_chars(bufstruc, req) \
  68.   if ((bufstruc.end - bufstruc.ptr + (req)) >= bufstruc.size) \
  69. {\
  70.          int cur_chars = bufstruc.end - bufstruc.ptr;\
  71.          bufstruc.size *= 2;\
  72.          bufstruc.ptr = xrealloc (bufstruc.ptr,bufstruc.size);\
  73.          bufstruc.end = bufstruc.ptr + cur_chars;\
  74. }
  75.  
  76. /* True if there is an embedded comment on this code line */
  77. int embedded_comment_on_line;
  78.  
  79. int else_or_endif;
  80.  
  81. /* structure indentation levels */
  82. int *di_stack;
  83.  
  84. /* Currently allocated size of di_stack.  */
  85. int di_stack_alloc;
  86.  
  87. /* when this is positive, we have seen a ? without
  88.    the matching : in a <c>?<s>:<s> construct */
  89. int squest;
  90.  
  91. #define CHECK_CODE_SIZE \
  92.     if (e_code >= l_code) { \
  93.         register nsize = l_code-s_code+400; \
  94.         codebuf = (char *) xrealloc (codebuf, nsize); \
  95.         e_code = codebuf + (e_code-s_code) + 1; \
  96.         l_code = codebuf + nsize - 5; \
  97.         s_code = codebuf + 1; \
  98.     }
  99.  
  100. #define CHECK_LAB_SIZE \
  101.     if (e_lab >= l_lab) { \
  102.         register nsize = l_lab-s_lab+400; \
  103.         labbuf = (char *) xrealloc (labbuf, nsize); \
  104.         e_lab = labbuf + (e_lab-s_lab) + 1; \
  105.         l_lab = labbuf + nsize - 5; \
  106.         s_lab = labbuf + 1; \
  107.     }
  108.  
  109. static void
  110. indent (this_file)
  111.      struct file_buffer *this_file;
  112. {
  113.   register int i;
  114.   enum codes hd_type;
  115.   register char *t_ptr;
  116.   enum codes type_code;
  117.  
  118.   /* current indentation for declarations */
  119.   int dec_ind = 0;
  120.  
  121.   /* used when buffering up comments to remember that
  122.      a newline was passed over */
  123.   int flushed_nl = 0;        
  124.   int force_nl = 0;
  125.  
  126.   /* true when we've just see a case; determines what to do
  127.      with the following colon */
  128.   int scase = 0;
  129.  
  130.   /* true when in the expression part of if(...), while(...), etc. */
  131.   int sp_sw = 0;
  132.  
  133.   /* True if we have just encountered the end of an if (...), etc. (i.e. the
  134.      ')' of the if (...) was the last token).  The variable is set to 2 in
  135.      the middle of the main token reading loop and is decremented at the
  136.      beginning of the loop, so it will reach zero when the second token after
  137.      the ')' is read.  */
  138.   int last_token_ends_sp = 0;
  139.  
  140.   /* true iff last keyword was an else */
  141.   int last_else = 0;
  142.  
  143.  
  144.   in_prog = in_prog_pos = this_file->data;
  145.   in_prog_size = this_file->size;
  146.  
  147.   hd_type = code_eof;
  148.   dec_ind = 0;
  149.   last_token_ends_sp = false;
  150.   last_else = false;
  151.   sp_sw = force_nl = false;
  152.   scase = false;
  153.   squest = false;
  154.  
  155. #if 0
  156.   if (com_ind <= 1)
  157.     com_ind = 2;        /* dont put normal comments before column 2 */
  158. #endif
  159.  
  160.   if (troff)
  161.     {
  162.       if (bodyf.font[0] == 0)
  163.     parsefont (&bodyf, "R");
  164.       if (scomf.font[0] == 0)
  165.     parsefont (&scomf, "I");
  166.       if (blkcomf.font[0] == 0)
  167.     blkcomf = scomf, blkcomf.size += 2;
  168.       if (boxcomf.font[0] == 0)
  169.     boxcomf = blkcomf;
  170.       if (stringf.font[0] == 0)
  171.     parsefont (&stringf, "L");
  172.       if (keywordf.font[0] == 0)
  173.     parsefont (&keywordf, "B");
  174.       writefdef (&bodyf, 'B');
  175.       writefdef (&scomf, 'C');
  176.       writefdef (&blkcomf, 'L');
  177.       writefdef (&boxcomf, 'X');
  178.       writefdef (&stringf, 'S');
  179.       writefdef (&keywordf, 'K');
  180.     }
  181.   if (block_comment_max_col <= 0)
  182.     block_comment_max_col = max_col;
  183.   if (decl_com_ind <= 0)    /* if not specified by user, set this */
  184.     decl_com_ind =
  185.       ljust_decl ? (com_ind <= 10 ? 2 : com_ind - 8) : com_ind;
  186.   if (continuation_indent == 0)
  187.     continuation_indent = ind_size;
  188.   fill_buffer ();        /* get first batch of stuff into input buffer */
  189.  
  190. #if 0
  191.   parse (semicolon);
  192. #endif
  193.  
  194.   {
  195.     register char *p = buf_ptr;
  196.     register col = 1;
  197.  
  198.     while (1)
  199.       {
  200.     if (*p == ' ')
  201.       col++;
  202.     else if (*p == TAB)
  203.       col = tabsize - (col % tabsize) + 1;
  204.     else if (*p == EOL)
  205.       col = 1;
  206.     else
  207.       break;
  208.  
  209.     p++;
  210.       }
  211.  
  212. #if 0
  213.     if (col > ind_size)
  214.       parser_state_tos->ind_level = parser_state_tos->i_l_follow = col;
  215. #endif/
  216.   }
  217.  
  218.   if (troff)
  219.     {
  220.       register char *p = in_name, *beg = in_name;
  221.  
  222.       while (*p)
  223. #ifdef macintosh
  224.     if (*p++ == ':')
  225. #else
  226.     if (*p++ == '/')
  227. #endif
  228.       beg = p;
  229.       fprintf (output, ".Fn \"%s\"\n", beg);
  230.     }
  231.   /* START OF MAIN LOOP */
  232.  
  233.   while (1)
  234.     {                /* this is the main loop.  it will go until
  235.                    we reach eof */
  236.       int is_procname;
  237.  
  238.       type_code = lexi ();    /* lexi reads one token.  "token" points to
  239.                    the actual characters. lexi returns a code
  240.                    indicating the type of token */
  241.  
  242.       if (last_token_ends_sp > 0)
  243.     last_token_ends_sp--;
  244.       is_procname = parser_state_tos->procname[0];
  245.  
  246.       /* The following code moves everything following an if (), while (),
  247.          else, etc. up to the start of the following stmt to a buffer. This
  248.          allows proper handling of both kinds of brace placement. */
  249.  
  250.       flushed_nl = false;
  251.       while (parser_state_tos->search_brace)
  252.     {
  253.       /* After scanning an if(), while (), etc., it might be necessary to
  254.          keep track of the text between the if() and the start of the
  255.          statement which follows.  Use save_com to do so.  */
  256.  
  257.       switch (type_code)
  258.         {
  259.         case newline:
  260.           ++line_no;
  261.           flushed_nl = true;
  262.         case form_feed:
  263.           break;        /* form feeds and newlines found here will be
  264.                    ignored */
  265.  
  266.         case lbrace:    /* this is a brace that starts the compound
  267.                    stmt */
  268.           if (save_com.end == save_com.ptr)
  269.         {
  270.           /* ignore buffering if a comment wasnt stored up */
  271.           parser_state_tos->search_brace = false;
  272.           goto check_type;
  273.         }
  274.           /* We need to put the '{' back into save_com somewhere.  */
  275.           if (btype_2)
  276.         /* Put it at the beginning, e.g. if (foo) { / * comment here *
  277.            / */
  278.  
  279.         save_com.ptr[0] = '{';
  280.  
  281.           else
  282.         {
  283.           /* Put it at the end, e.g. if (foo) / * comment here * / { */
  284.  
  285.           /* Putting in this newline causes a dump_line to occur
  286.              right after the comment, thus insuring that it will be
  287.              put in the correct column.  */
  288.           *save_com.end++ = EOL;
  289.           *save_com.end++ = '{';
  290.         }
  291.  
  292.           /* Go to common code to get out of this loop.  */
  293.           goto sw_buffer;
  294.  
  295.         case comment:    /* we have a comment, so we must copy it into
  296.                    the buffer */
  297.           if (!flushed_nl || save_com.end != save_com.ptr)
  298.         {
  299.           need_chars (save_com, 10);
  300.           if (save_com.end == save_com.ptr)
  301.             {        /* if this is the first comment, we must set
  302.                    up the buffer */
  303.               save_com.ptr[0] = save_com.ptr[1] = ' ';
  304.               save_com.end = save_com.ptr + 2;
  305.             }
  306.           else
  307.             {
  308.               *save_com.end++ = EOL;    /* add newline between
  309.                            comments */
  310.               *save_com.end++ = ' ';
  311.               --line_no;
  312.             }
  313.           *save_com.end++ = '/';    /* copy in start of comment */
  314.           *save_com.end++ = '*';
  315.  
  316.           for (;;)
  317.             {        /* loop until we get to the end of the
  318.                    comment */
  319.               /* make sure there is room for this character and
  320.                  (while we're at it) the '/' we might add at the end
  321.                  of the loop. */
  322.               need_chars (save_com, 2);
  323.               *save_com.end = *buf_ptr++;
  324.               if (buf_ptr >= buf_end)
  325.             {
  326.               fill_buffer ();
  327.               if (had_eof)
  328.                 {
  329.                   diag (1, "Unclosed comment", 0, 0);
  330.                   exit (1);
  331.                 }
  332.             }
  333.  
  334.               if (*save_com.end++ == '*' && *buf_ptr == '/')
  335.             break;    /* we are at end of comment */
  336.  
  337.             }
  338.           *save_com.end++ = '/';    /* add ending slash */
  339.           if (++buf_ptr >= buf_end)    /* get past / in buffer */
  340.             fill_buffer ();
  341.           break;
  342.         }
  343.         default:        /* it is the start of a normal statment */
  344.           if (flushed_nl)    /* if we flushed a newline, make sure it is
  345.                    put back */
  346.         force_nl = true;
  347.           if ((type_code == sp_paren && *token == 'i'
  348.            && last_else && else_if)
  349.           ||
  350.           (type_code == sp_nparen && *token == 'e'
  351.            && e_code != s_code && e_code[-1] == '}'))
  352.         force_nl = false;
  353.  
  354.           if (save_com.end == save_com.ptr)
  355.         {
  356.           /* ignore buffering if comment wasnt saved up */
  357.           parser_state_tos->search_brace = false;
  358.           goto check_type;
  359.         }
  360.           if (force_nl)
  361.         {        /* if we should insert a nl here, put it into
  362.                    the buffer */
  363.           force_nl = false;
  364.           --line_no;    /* this will be re-increased when the nl is
  365.                    read from the buffer */
  366.           need_chars (save_com, 2);
  367.           *save_com.end++ = EOL;
  368.           *save_com.end++ = ' ';
  369.           if (verbose && !flushed_nl)    /* print error msg if the
  370.                            line was not already
  371.                            broken */
  372.             diag (0, "Line broken", 0, 0);
  373.           flushed_nl = false;
  374.         }
  375.           for (t_ptr = token; t_ptr < token_end; ++t_ptr)
  376.         {
  377.           need_chars (save_com, 1);
  378.           *save_com.end++ = *t_ptr;    /* copy token into temp
  379.                            buffer */
  380.         }
  381.           parser_state_tos->procname = "\0";
  382.  
  383.         sw_buffer:
  384.           parser_state_tos->search_brace = false;    /* stop looking for
  385.                                start of stmt */
  386.           bp_save = buf_ptr;/* save current input buffer */
  387.           be_save = buf_end;
  388.           buf_ptr = save_com.ptr;    /* fix so that subsequent calls to
  389.                        lexi will take tokens out of
  390.                        save_com */
  391.           need_chars (save_com, 1);
  392.           *save_com.end++ = ' ';    /* add trailing blank, just in case */
  393.           buf_end = save_com.end;
  394.           save_com.end = save_com.ptr;    /* make save_com empty */
  395.           break;
  396.         }            /* end of switch */
  397.       /* we must make this check, just in case there was an unexpected
  398.          EOF */
  399.       if (type_code != code_eof)
  400.         type_code = lexi ();/* read another token */
  401.       /* if (parser_state_tos->search_brace)
  402.          parser_state_tos->procname[0] = 0; */
  403.       if ((is_procname = parser_state_tos->procname[0]) && flushed_nl
  404.           && !procnames_start_line && parser_state_tos->in_decl
  405.           && type_code == ident)
  406.         flushed_nl = 0;
  407.     }            /* end of while (search_brace) */
  408.       last_else = 0;
  409.  
  410.     check_type:
  411.       if (type_code == code_eof)
  412.     {            /* we got eof */
  413.       if (s_lab != e_lab || s_code != e_code
  414.           || s_com != e_com)/* must dump end of line */
  415.         dump_line ();
  416.       if (parser_state_tos->tos > 1)    /* check for balanced braces */
  417.         diag (1, "Stuff missing from end of file.", 0, 0);
  418.  
  419.       if (verbose)
  420.         {
  421.           printf ("There were %d output lines and %d comments\n",
  422.               (int) out_lines, (int) out_coms);
  423.           printf ("(Lines with comments)/(Lines with code): %6.3f\n",
  424.               (1.0 * com_lines) / code_lines);
  425.         }
  426.       fflush (output);
  427.       if (found_err)
  428.         exit (found_err);
  429.  
  430.       return;
  431.     }
  432.  
  433.       if ((type_code != comment) &&
  434.       (type_code != cplus_comment) &&
  435.       (type_code != newline) &&
  436.       (type_code != preesc) &&
  437.       (type_code != form_feed))
  438.     {
  439.       if (force_nl &&
  440.           (type_code != semicolon) &&
  441.           (type_code != lbrace || !btype_2))
  442.         {
  443.           /* we should force a broken line here */
  444.           if (verbose && !flushed_nl)
  445.         diag (0, "Line broken", 0, 0);
  446.           flushed_nl = false;
  447.           dump_line ();
  448.           parser_state_tos->want_blank = false;    /* dont insert blank at
  449.                                line start */
  450.           force_nl = false;
  451.         }
  452.       parser_state_tos->in_stmt = true;    /* turn on flag which causes
  453.                            an extra level of
  454.                            indentation. this is
  455.                            turned off by a ; or } */
  456.       if (s_com != e_com)
  457.         {            /* the turkey has embedded a comment in a
  458.                    line. Move it from the com buffer to the
  459.                    code buffer.  */
  460.           /* Do not add a space before the comment if it is the first
  461.              thing on the line.  */
  462.           if (e_code != s_code)
  463.         {
  464.           *e_code++ = ' ';
  465.           embedded_comment_on_line = 2;
  466.         }
  467.           else
  468.         embedded_comment_on_line = 1;
  469.  
  470.           for (t_ptr = s_com; *t_ptr; ++t_ptr)
  471.         {
  472.           CHECK_CODE_SIZE;
  473.           *e_code++ = *t_ptr;
  474.         }
  475.           *e_code++ = ' ';
  476.           *e_code = '\0';    /* null terminate code sect */
  477.           parser_state_tos->want_blank = false;
  478.           e_com = s_com;
  479.         }
  480.     }
  481.       else if (type_code != comment
  482.            && type_code != cplus_comment)
  483.     /* preserve force_nl thru a comment but
  484.        cancel forced newline after newline, form feed, etc */
  485.        force_nl = false;
  486.  
  487.  
  488.  
  489.       /*-----------------------------------------------------*\
  490.       |       do switch on type of token scanned              |
  491.       \*-----------------------------------------------------*/
  492.       CHECK_CODE_SIZE;
  493.       switch (type_code)
  494.     {            /* now, decide what to do with the token */
  495.  
  496.     case form_feed:    /* found a form feed in line */
  497.       parser_state_tos->use_ff = true;    /* a form feed is treated
  498.                            much like a newline */
  499.       dump_line ();
  500.       parser_state_tos->want_blank = false;
  501.       break;
  502.  
  503.     case newline:
  504.       if ((parser_state_tos->last_token != comma
  505.            || !leave_comma || !break_comma
  506.            || parser_state_tos->p_l_follow > 0
  507.            || parser_state_tos->block_init
  508.            || s_com != e_com)
  509.           && (parser_state_tos->last_token != rbrace || !btype_2)
  510.           || (compute_code_target () + (e_code - s_code)
  511.           > max_col - tabsize))
  512.         {
  513.           dump_line ();
  514.           parser_state_tos->want_blank = false;
  515.         }
  516.       /* If we were on the line with a #else or a #endif, we aren't
  517.          anymore.  */
  518.       else_or_endif = false;
  519.       ++line_no;        /* keep track of input line number */
  520.       break;
  521.  
  522.     case lparen:
  523.       /* Braces in initializer lists should be put on new lines. This is
  524.          necessary so that -gnu does not cause things like char
  525.          *this_is_a_string_array[] = { "foo", "this_string_does_not_fit",
  526.          "nor_does_this_rather_long_string" } which is what happens
  527.          because we are trying to line the strings up with the
  528.          parentheses, and those that are too long are moved to the right
  529.          an ugly amount.
  530.     
  531.          However, if the current line is empty, the left brace is
  532.          already on a new line, so don't molest it.  */
  533.       if (token[0] == '{'
  534.           && (s_code != e_code || s_com != e_com || s_lab != e_lab))
  535.         {
  536.           dump_line ();
  537.           /* Do not put a space before the '{'.  */
  538.           parser_state_tos->want_blank = false;
  539.         }
  540.  
  541.       /* Count parens so we know how deep we are.  */
  542.       if (++parser_state_tos->p_l_follow
  543.           >= parser_state_tos->paren_indents_size)
  544.         {
  545.           parser_state_tos->paren_indents_size *= 2;
  546.           parser_state_tos->paren_indents = (short *)
  547.         xrealloc ((char *) parser_state_tos->paren_indents,
  548.               parser_state_tos->paren_indents_size
  549.               * sizeof (short));
  550.         }
  551.       parser_state_tos->paren_depth++;
  552.       if (parser_state_tos->want_blank && *token != '['
  553.           && (parser_state_tos->last_token != ident || proc_calls_space
  554.           || (parser_state_tos->its_a_keyword
  555.               && (!parser_state_tos->sizeof_keyword
  556.               || blank_after_sizeof))))
  557.         *e_code++ = ' ';
  558.  
  559.       if (parser_state_tos->in_decl && !parser_state_tos->block_init)
  560.         {
  561.           if (troff
  562.           && !parser_state_tos->dumped_decl_indent
  563.           && !is_procname
  564.           && parser_state_tos->last_token == decl)
  565.         {
  566.           parser_state_tos->dumped_decl_indent = 1;
  567.           sprintf (e_code, "\n.Du %dp+\200p \"%.*s\"\n",
  568.                (int) (dec_ind * 7),
  569.                token_end - token, token);
  570.           e_code += strlen (e_code);
  571.         }
  572.           else if (*token != '[')
  573.         {
  574.           while ((e_code - s_code) < dec_ind)
  575.             {
  576.               CHECK_CODE_SIZE;
  577.               *e_code++ = ' ';
  578.             }
  579.           *e_code++ = token[0];
  580.         }
  581.           else
  582.         *e_code++ = token[0];
  583.         }
  584.       else
  585.         *e_code++ = token[0];
  586.  
  587.       parser_state_tos->paren_indents[parser_state_tos->p_l_follow - 1]
  588.         = e_code - s_code;
  589.       if (sp_sw && parser_state_tos->p_l_follow == 1
  590.           && extra_expression_indent
  591.           && parser_state_tos->paren_indents[0] < 2 * ind_size)
  592.         parser_state_tos->paren_indents[0] = 2 * ind_size;
  593.       parser_state_tos->want_blank = false;
  594.  
  595.       if (parser_state_tos->in_or_st
  596.           && *token == '('
  597.           && parser_state_tos->tos <= 2)
  598.         {
  599.           /* this is a kluge to make sure that declarations will be
  600.              aligned right if proc decl has an explicit type on it, i.e.
  601.              "int a(x) {..." */
  602.           parse_lparen_in_decl ();
  603.  
  604.           /* Turn off flag for structure decl or initialization.  */
  605.           parser_state_tos->in_or_st = false;
  606.         }
  607.       if (parser_state_tos->sizeof_keyword)
  608.         parser_state_tos->sizeof_mask |= 1 << parser_state_tos->p_l_follow;
  609.  
  610.       /* The '(' that starts a cast can never be preceeded by an
  611.          indentifier or decl.  */
  612.       if (parser_state_tos->last_token == decl
  613.           || (parser_state_tos->last_token == ident
  614.           && parser_state_tos->last_rw != rw_return))
  615.         parser_state_tos->noncast_mask |=
  616.           1 << parser_state_tos->p_l_follow;
  617.       else
  618.         parser_state_tos->noncast_mask &=
  619.           ~(1 << parser_state_tos->p_l_follow);
  620.  
  621.       break;
  622.  
  623.     case rparen:
  624.       parser_state_tos->paren_depth--;
  625.       if (parser_state_tos->cast_mask
  626.           & (1 << parser_state_tos->p_l_follow)
  627.           & ~parser_state_tos->sizeof_mask)
  628.         {
  629.           parser_state_tos->last_u_d = true;
  630.           parser_state_tos->cast_mask &=
  631.         (1 << parser_state_tos->p_l_follow) - 1;
  632.           if (!parser_state_tos->cast_mask && cast_space)
  633.         parser_state_tos->want_blank = true;
  634.           else
  635.         parser_state_tos->want_blank = false;
  636.         }
  637.       else if (parser_state_tos->in_decl
  638.            && ! parser_state_tos->block_init
  639.            && parser_state_tos->paren_depth == 0)
  640.         parser_state_tos->want_blank = true;
  641.  
  642.       parser_state_tos->sizeof_mask
  643.         &= (1 << parser_state_tos->p_l_follow) - 1;
  644.       if (--parser_state_tos->p_l_follow < 0)
  645.         {
  646.           parser_state_tos->p_l_follow = 0;
  647.           diag (0, "Extra %c", (int) *token, 0);
  648.         }
  649.  
  650.       /* if the paren starts the line, then indent it */
  651.       if (e_code == s_code)
  652.         {
  653.           int level = parser_state_tos->p_l_follow;
  654.           parser_state_tos->paren_level = level;
  655.           if (level > 0)
  656.         paren_target = -parser_state_tos->paren_indents[level - 1];
  657.           else
  658.         paren_target = 0;
  659.         }
  660.       *e_code++ = token[0];
  661.  
  662. #if 0
  663.       if (!parser_state_tos->cast_mask || cast_space)
  664.         parser_state_tos->want_blank = true;
  665. #endif
  666.  
  667.       /* check for end of if (...), or some such */
  668.       if (sp_sw && (parser_state_tos->p_l_follow == 0))
  669.         {
  670.  
  671.           /* Indicate that we have just left the parenthesized expression
  672.              of a while, if, or for, unless we are getting out of the
  673.              parenthesized expression of the while of a do-while loop.
  674.              (do-while is different because a semicolon immediately
  675.              following this will not indicate a null loop body).  */
  676.           if (parser_state_tos->p_stack[parser_state_tos->tos]
  677.           != dohead)
  678.         last_token_ends_sp = 2;
  679.           sp_sw = false;
  680.           force_nl = true;    /* must force newline after if */
  681.           parser_state_tos->last_u_d = true;    /* inform lexi that a
  682.                                following operator is
  683.                                unary */
  684.           parser_state_tos->in_stmt = false;    /* dont use stmt
  685.                                continuation
  686.                                indentation */
  687.  
  688.           parse (hd_type);    /* let parser worry about if, or whatever */
  689.         }
  690.       parser_state_tos->search_brace = btype_2;    /* this should insure
  691.                                that constructs such
  692.                                as main(){...} and
  693.                                int[]{...} have their
  694.                                braces put in the
  695.                                right place */
  696.       break;
  697.  
  698.     case unary_op:        /* this could be any unary operation */
  699.       if (parser_state_tos->want_blank)
  700.         *e_code++ = ' ';
  701.  
  702.       if (troff
  703.           && !parser_state_tos->dumped_decl_indent
  704.           && parser_state_tos->in_decl && !is_procname)
  705.         {
  706.           sprintf (e_code, "\n.Du %dp+\200p \"%.*s\"\n",
  707.                (int) (dec_ind * 7),
  708.                token_end - token, token);
  709.           parser_state_tos->dumped_decl_indent = 1;
  710.           e_code += strlen (e_code);
  711.         }
  712.       else
  713.         {
  714.           char *res = token;
  715.           char *res_end = token_end;
  716.  
  717.           /* if this is a unary op in a declaration, we should
  718.          indent this token */
  719.           if (parser_state_tos->paren_depth == 0
  720.           && parser_state_tos->in_decl
  721.           && !parser_state_tos->block_init)
  722.         {
  723.           while ((e_code - s_code) < (dec_ind - (token_end - token)))
  724.             {
  725.               CHECK_CODE_SIZE;
  726.               *e_code++ = ' ';
  727.             }
  728.         }
  729.  
  730.           if (troff && token[0] == '-' && token[1] == '>')
  731.         {
  732.           static char resval[] = "\\(->";
  733.           res = resval;
  734.           res_end = res + sizeof (resval);
  735.         }
  736.  
  737.           for (t_ptr = res; t_ptr < res_end; ++t_ptr)
  738.         {
  739.           CHECK_CODE_SIZE;
  740.           *e_code++ = *t_ptr;
  741.         }
  742.         }
  743.       parser_state_tos->want_blank = false;
  744.       break;
  745.  
  746.     case binary_op:    /* any binary operation */
  747.       if (parser_state_tos->want_blank
  748.           || (e_code > s_code && *e_code != ' '))
  749.         *e_code++ = ' ';
  750.  
  751.       {
  752.         char *res = token;
  753.         char *res_end = token_end;
  754. #define set_res(str) \
  755.           {\
  756.         static char resval[] = str;\
  757.         res = resval;\
  758.         res_end = res + sizeof(resval);\
  759.           }
  760.  
  761.         if (troff)
  762.           switch (token[0])
  763.         {
  764.         case '<':
  765.           if (token[1] == '=')
  766.             set_res ("\\(<=");
  767.           break;
  768.         case '>':
  769.           if (token[1] == '=')
  770.             set_res ("\\(>=");
  771.           break;
  772.         case '!':
  773.           if (token[1] == '=')
  774.             set_res ("\\(!=");
  775.           break;
  776.         case '|':
  777.           if (token[1] == '|')
  778.             {
  779.               set_res ("\\(br\\(br");
  780.             }
  781.           else if (token[1] == 0)
  782.             set_res ("\\(br");
  783.           break;
  784.         }
  785.  
  786.         for (t_ptr = res; t_ptr < res_end; ++t_ptr)
  787.           {
  788.         CHECK_CODE_SIZE;
  789.         *e_code++ = *t_ptr;    /* move the operator */
  790.           }
  791.       }
  792.       parser_state_tos->want_blank = true;
  793.       break;
  794.  
  795.     case postop:        /* got a trailing ++ or -- */
  796.       *e_code++ = token[0];
  797.       *e_code++ = token[1];
  798.       parser_state_tos->want_blank = true;
  799.       break;
  800.  
  801.     case question:        /* got a ? */
  802.       squest++;        /* this will be used when a later colon
  803.                    appears so we can distinguish the
  804.                    <c>?<n>:<n> construct */
  805.       if (parser_state_tos->want_blank)
  806.         *e_code++ = ' ';
  807.       *e_code++ = '?';
  808.       parser_state_tos->want_blank = true;
  809.       break;
  810.  
  811.     case casestmt:        /* got word 'case' or 'default' */
  812.       scase = true;        /* so we can process the later colon
  813.                    properly */
  814.       goto copy_id;
  815.  
  816.     case colon:        /* got a ':' */
  817.       if (squest > 0)
  818.         {            /* it is part of the <c>?<n>: <n> construct */
  819.           --squest;
  820.           if (parser_state_tos->want_blank)
  821.         *e_code++ = ' ';
  822.           *e_code++ = ':';
  823.           parser_state_tos->want_blank = true;
  824.           break;
  825.         }
  826.       if (parser_state_tos->in_decl)
  827.         {
  828.           *e_code++ = ':';
  829.           parser_state_tos->want_blank = false;
  830.           break;
  831.         }
  832.       parser_state_tos->in_stmt = false;    /* seeing a label does not
  833.                            imply we are in a stmt */
  834.       for (t_ptr = s_code; *t_ptr; ++t_ptr)
  835.         *e_lab++ = *t_ptr;    /* turn everything so far into a label */
  836.       e_code = s_code;
  837.       *e_lab++ = ':';
  838.       *e_lab++ = ' ';
  839.       *e_lab = '\0';
  840.       /* parser_state_tos->pcas e will be used by dump_line to decide
  841.          how to indent the label. force_nl will force a case n: to be
  842.          on a line by itself */
  843.       force_nl = parser_state_tos->pcase = scase;
  844.       scase = false;
  845.       parser_state_tos->want_blank = false;
  846.       break;
  847.  
  848.     case semicolon:
  849.       /* we are not in an initialization or structure declaration */
  850.       parser_state_tos->in_or_st = false;
  851.       scase = false;
  852.       squest = 0;
  853.       /* The following code doesn't seem to do much good. Just because
  854.          we've found something like extern int foo();    or int (*foo)();
  855.          doesn't mean we are out of a declaration.  Now if it was serving
  856.          some purpose we'll have to address that.... if
  857.          (parser_state_tos->last_token == rparen)
  858.          parser_state_tos->in_parameter_declaration = 0; */
  859.       parser_state_tos->cast_mask = 0;
  860.       parser_state_tos->sizeof_mask = 0;
  861.       parser_state_tos->block_init = 0;
  862.       parser_state_tos->block_init_level = 0;
  863.       parser_state_tos->just_saw_decl--;
  864.  
  865.       if (parser_state_tos->in_decl
  866.           && s_code == e_code
  867.           && !parser_state_tos->block_init)
  868.         while ((e_code - s_code) < (dec_ind - 1))
  869.           {
  870.         CHECK_CODE_SIZE;
  871.         *e_code++ = ' ';
  872.           }
  873.  
  874.       /* if we were in a first level structure declaration,
  875.          we aren't any more */
  876.       parser_state_tos->in_decl = (parser_state_tos->dec_nest > 0);
  877.  
  878. #if 0    /* Changed to allow "{}" inside parens, as when
  879.        passed to a macro.  -jla */
  880.       if ((!sp_sw || hd_type != forstmt)
  881.           && parser_state_tos->p_l_follow > 0)
  882.         {
  883.  
  884.           /* This should be true iff there were unbalanced parens in the
  885.              stmt.  It is a bit complicated, because the semicolon might
  886.              be in a for stmt */
  887.           diag (1, "Unbalanced parens", 0, 0);
  888.           parser_state_tos->p_l_follow = 0;
  889.           if (sp_sw)
  890.         {        /* this is a check for a if, while, etc. with
  891.                    unbalanced parens */
  892.           sp_sw = false;
  893.           parse (hd_type);    /* dont lose the if, or whatever */
  894.         }
  895.         }
  896. #endif
  897.  
  898.       /* If we have a semicolon following an if, while, or for, and the
  899.          user wants us to, we should insert a space (to show that there
  900.          is a null statement there).  */
  901.       if (last_token_ends_sp && space_sp_semicolon)
  902.         {
  903.           *e_code++ = ' ';
  904.         }
  905.       *e_code++ = ';';
  906.       parser_state_tos->want_blank = true;
  907.       /* we are no longer in the middle of a stmt */
  908.       parser_state_tos->in_stmt = (parser_state_tos->p_l_follow > 0);
  909.  
  910.       if (!sp_sw)
  911.         {            /* if not if for (;;) */
  912.           parse (semicolon);/* let parser know about end of stmt */
  913.           force_nl = true;    /* force newline after a end of stmt */
  914.         }
  915.       break;
  916.  
  917.     case lbrace:        /* got a '{' */
  918.       parser_state_tos->in_stmt = false;    /* dont indent the {} */
  919.       if (!parser_state_tos->block_init)
  920.         force_nl = true;    /* force other stuff on same line as '{' onto
  921.                    new line */
  922.       else if (parser_state_tos->block_init_level <= 0)
  923.         parser_state_tos->block_init_level = 1;
  924.       else
  925.         parser_state_tos->block_init_level++;
  926.  
  927.       if (s_code != e_code && !parser_state_tos->block_init)
  928.         {
  929.           if (!btype_2)
  930.         {
  931.           dump_line ();
  932.           parser_state_tos->want_blank = false;
  933.         }
  934.           else
  935.         {
  936.           if (parser_state_tos->in_parameter_declaration
  937.               && !parser_state_tos->in_or_st)
  938.             {
  939.               parser_state_tos->i_l_follow = 0;
  940.               dump_line ();
  941.               parser_state_tos->want_blank = false;
  942.             }
  943.           else
  944.             parser_state_tos->want_blank = true;
  945.         }
  946.         }
  947.       if (parser_state_tos->in_parameter_declaration)
  948.         prefix_blankline_requested = 0;
  949.  
  950. #if 0    /* Changed to allow "{}" inside parens, as when
  951.        passed to a macro.  -jla */
  952.       if (parser_state_tos->p_l_follow > 0)
  953.         {            /* check for preceeding unbalanced parens */
  954.           diag (1, "Unbalanced parens", 0, 0);
  955.           parser_state_tos->p_l_follow = 0;
  956.           if (sp_sw)
  957.         {        /* check for unclosed if, for, etc. */
  958.           sp_sw = false;
  959.           parse (hd_type);
  960.           parser_state_tos->ind_level = parser_state_tos->i_l_follow;
  961.         }
  962.         }
  963. #endif
  964.       if (s_code == e_code)
  965.         parser_state_tos->ind_stmt = false;    /* dont put extra indentation
  966.                            on line with '{' */
  967.       if (parser_state_tos->in_decl && parser_state_tos->in_or_st)
  968.         {
  969.           /* This is a structure declaration.  */
  970.           if (parser_state_tos->dec_nest >= di_stack_alloc)
  971.         {
  972.           di_stack_alloc *= 2;
  973.           di_stack = (int *)
  974.             xrealloc ((char *) di_stack,
  975.                   di_stack_alloc * sizeof (*di_stack));
  976.         }
  977.           di_stack[parser_state_tos->dec_nest++] = dec_ind;
  978.           /* ?        dec_ind = 0; */
  979.         }
  980.       else
  981.         {
  982.           parser_state_tos->in_decl = false;
  983.           parser_state_tos->decl_on_line = false;    /* we cant be in the
  984.                                middle of a
  985.                                declaration, so dont
  986.                                do special
  987.                                indentation of
  988.                                comments */
  989.  
  990. #if 0                /* Doesn't work currently. */
  991.           if (blanklines_after_declarations_at_proctop
  992.           && parser_state_tos->in_parameter_declaration)
  993.         postfix_blankline_requested = 1;
  994. #endif
  995.           parser_state_tos->in_parameter_declaration = 0;
  996.         }
  997.       dec_ind = 0;
  998.  
  999.       /* We are no longer looking for an initializer or structure. Needed
  1000.          so that the '=' in "enum bar {a = 1" does not get interpreted as
  1001.          the start of an initializer.  */
  1002.       parser_state_tos->in_or_st = false;
  1003.  
  1004.       parse (lbrace);    /* let parser know about this */
  1005.       if (parser_state_tos->want_blank)    /* put a blank before '{' if
  1006.                            '{' is not at start of
  1007.                            line */
  1008.         *e_code++ = ' ';
  1009.       parser_state_tos->want_blank = false;
  1010.       *e_code++ = '{';
  1011.       parser_state_tos->just_saw_decl = 0;
  1012.       break;
  1013.  
  1014.     case rbrace:        /* got a '}' */
  1015.       /* semicolons can be omitted in declarations */
  1016.       if (parser_state_tos->p_stack[parser_state_tos->tos] == decl
  1017.           && !parser_state_tos->block_init)
  1018.         parse (semicolon);
  1019. #if 0    /* Changed to allow "{}" inside parens, as when
  1020.        passed to a macro.  -jla */
  1021.       if (parser_state_tos->p_l_follow)
  1022.         {            /* check for unclosed if, for, else. */
  1023.           diag (1, "Unbalanced parens", 0, 0);
  1024.           parser_state_tos->p_l_follow = 0;
  1025.           sp_sw = false;
  1026.         }
  1027. #endif
  1028.  
  1029.       parser_state_tos->just_saw_decl = 0;
  1030.       parser_state_tos->block_init_level--;
  1031.       if (s_code != e_code && !parser_state_tos->block_init)
  1032.         {            /* '}' must be first on line */
  1033.           if (verbose)
  1034.         diag (0, "Line broken", 0, 0);
  1035.           dump_line ();
  1036.         }
  1037.       *e_code++ = '}';
  1038.       parser_state_tos->want_blank = true;
  1039.       parser_state_tos->in_stmt = parser_state_tos->ind_stmt = false;
  1040.       if (parser_state_tos->dec_nest > 0)
  1041.         {            /* we are in multi-level structure
  1042.                    declaration */
  1043.           dec_ind = di_stack[--parser_state_tos->dec_nest];
  1044.           if (parser_state_tos->dec_nest == 0
  1045.           && !parser_state_tos->in_parameter_declaration)
  1046.         parser_state_tos->just_saw_decl = 2;
  1047.           parser_state_tos->in_decl = true;
  1048.         }
  1049.       prefix_blankline_requested = 0;
  1050.       parse (rbrace);    /* let parser know about this */
  1051.       parser_state_tos->search_brace
  1052.         = (cuddle_else
  1053.          && parser_state_tos->p_stack[parser_state_tos->tos] == ifhead);
  1054.  
  1055.       if ((parser_state_tos->p_stack[parser_state_tos->tos] == stmtl
  1056.            && ((parser_state_tos->last_rw != rw_struct_like
  1057.             && parser_state_tos->last_rw != rw_decl)
  1058.            || ! btype_2))
  1059.           || (parser_state_tos->p_stack[parser_state_tos->tos] == ifhead)
  1060.           || (parser_state_tos->p_stack[parser_state_tos->tos] == dohead
  1061.           && !btype_2))
  1062.         force_nl = true;
  1063.       else if (parser_state_tos->tos <= 1
  1064.            && blanklines_after_procs
  1065.            && parser_state_tos->dec_nest <= 0)
  1066.         postfix_blankline_requested = 1;
  1067.  
  1068. #if 0
  1069.       parser_state_tos->search_brace
  1070.         = (cuddle_else
  1071.            && parser_state_tos->p_stack[parser_state_tos->tos] == ifhead
  1072.            && (parser_state_tos->il[parser_state_tos->tos]
  1073.            >= parser_state_tos->ind_level));
  1074. #endif
  1075.  
  1076.       break;
  1077.  
  1078.     case swstmt:        /* got keyword "switch" */
  1079.       sp_sw = true;
  1080.       hd_type = swstmt;    /* keep this for when we have seen the
  1081.                    expression */
  1082.       parser_state_tos->in_decl = false;
  1083.       goto copy_id;        /* go move the token into buffer */
  1084.  
  1085.     case sp_paren:        /* token is if, while, for */
  1086.       sp_sw = true;        /* the interesting stuff is done after the
  1087.                    expression is scanned */
  1088.       hd_type = (*token == 'i' ? ifstmt :
  1089.              (*token == 'w' ? whilestmt : forstmt));
  1090.  
  1091.       /* remember the type of header for later use by parser */
  1092.       goto copy_id;        /* copy the token into line */
  1093.  
  1094.     case sp_nparen:    /* got else, do */
  1095.       parser_state_tos->in_stmt = false;
  1096.       if (*token == 'e')
  1097.         {
  1098.           if (e_code != s_code && (!cuddle_else || e_code[-1] != '}'))
  1099.         {
  1100.           if (verbose)
  1101.             diag (0, "Line broken", 0, 0);
  1102.           dump_line ();    /* make sure this starts a line */
  1103.           parser_state_tos->want_blank = false;
  1104.         }
  1105.           force_nl = true;    /* also, following stuff must go onto new
  1106.                    line */
  1107.           last_else = 1;
  1108.           parse (elselit);
  1109.         }
  1110.       else
  1111.         {
  1112.           if (e_code != s_code)
  1113.         {        /* make sure this starts a line */
  1114.           if (verbose)
  1115.             diag (0, "Line broken", 0, 0);
  1116.           dump_line ();
  1117.           parser_state_tos->want_blank = false;
  1118.         }
  1119.           force_nl = true;    /* also, following stuff must go onto new
  1120.                    line */
  1121.           last_else = 0;
  1122.           parse (dolit);
  1123.         }
  1124.       goto copy_id;        /* move the token into line */
  1125.  
  1126.     case decl:        /* we have a declaration type (int, register,
  1127.                    etc.) */
  1128.  
  1129.       if (! parser_state_tos->sizeof_mask)
  1130.         parse (decl);
  1131.  
  1132.       if (parser_state_tos->last_token == rparen
  1133.           && parser_state_tos->tos <= 1)
  1134.         {
  1135.           parser_state_tos->in_parameter_declaration = 1;
  1136.           if (s_code != e_code)
  1137.         {
  1138.           dump_line ();
  1139.           parser_state_tos->want_blank = false;
  1140.         }
  1141.         }
  1142.       if (parser_state_tos->in_parameter_declaration
  1143.           && indent_parameters
  1144.           && parser_state_tos->dec_nest == 0
  1145.           && parser_state_tos->p_l_follow == 0)
  1146.         {
  1147.           parser_state_tos->ind_level
  1148.         = parser_state_tos->i_l_follow = indent_parameters;
  1149.           parser_state_tos->ind_stmt = 0;
  1150.         }
  1151.  
  1152.       /* in_or_st set for struct or initialization decl. Don't set it if
  1153.          we're in ansi prototype */
  1154.       if (!parser_state_tos->paren_depth)
  1155.         parser_state_tos->in_or_st = true;
  1156.  
  1157.       parser_state_tos->in_decl = parser_state_tos->decl_on_line = true;
  1158. #if 0
  1159.       if (!parser_state_tos->in_or_st && parser_state_tos->dec_nest <= 0)
  1160. #endif
  1161.         if (parser_state_tos->dec_nest <= 0)
  1162.           parser_state_tos->just_saw_decl = 2;
  1163.       if (prefix_blankline_requested
  1164.           && (parser_state_tos->block_init != 0
  1165.           || parser_state_tos->block_init_level != -1
  1166.           || parser_state_tos->last_token != rbrace
  1167.           || e_code != s_code
  1168.           || e_lab != s_lab
  1169.           || e_com != s_com))
  1170.         prefix_blankline_requested = 0;
  1171.       i = token_end - token + 1;    /* get length of token plus 1 */
  1172.  
  1173.       /* dec_ind = e_code - s_code + (parser_state_tos->decl_indent>i ?
  1174.          parser_state_tos->decl_indent : i); */
  1175.       dec_ind = decl_indent > 0 ? decl_indent : i;
  1176.       goto copy_id;
  1177.  
  1178.     case ident:        /* got an identifier or constant */
  1179.       /* If we are in a declaration, we must indent identifier. But not
  1180.          inside the parentheses of an ANSI function declaration.  */
  1181.       if (parser_state_tos->in_decl
  1182.           && parser_state_tos->p_l_follow == 0
  1183.           && parser_state_tos->last_token != rbrace)
  1184.         {
  1185.           if (parser_state_tos->want_blank)
  1186.         *e_code++ = ' ';
  1187.           parser_state_tos->want_blank = false;
  1188.           if (is_procname == 0 || !procnames_start_line)
  1189.         {
  1190.           if (!parser_state_tos->block_init)
  1191.             if (troff && !parser_state_tos->dumped_decl_indent)
  1192.               {
  1193.             sprintf (e_code, "\n.De %dp+\200p\n",
  1194.                  (int) (dec_ind * 7));
  1195.             parser_state_tos->dumped_decl_indent = 1;
  1196.             e_code += strlen (e_code);
  1197.               }
  1198.             else
  1199.               while ((e_code - s_code) < dec_ind)
  1200.             {
  1201.               CHECK_CODE_SIZE;
  1202.               *e_code++ = ' ';
  1203.             }
  1204.         }
  1205.           else
  1206.         {
  1207.           if (dec_ind && s_code != e_code)
  1208.             dump_line ();
  1209.           dec_ind = 0;
  1210.           parser_state_tos->want_blank = false;
  1211.         }
  1212.         }
  1213.       else if (sp_sw && parser_state_tos->p_l_follow == 0)
  1214.         {
  1215.           sp_sw = false;
  1216.           force_nl = true;
  1217.           parser_state_tos->last_u_d = true;
  1218.           parser_state_tos->in_stmt = false;
  1219.           parse (hd_type);
  1220.         }
  1221.     copy_id:
  1222.       if (parser_state_tos->want_blank)
  1223.         *e_code++ = ' ';
  1224.       if (troff && parser_state_tos->its_a_keyword)
  1225.         {
  1226.           e_code = chfont (&bodyf, &keywordf, e_code);
  1227.           for (t_ptr = token; t_ptr < token_end; ++t_ptr)
  1228.         {
  1229.           CHECK_CODE_SIZE;
  1230.           *e_code++ = keywordf.allcaps && islower (*t_ptr)
  1231.             ? toupper (*t_ptr) : *t_ptr;
  1232.         }
  1233.           e_code = chfont (&keywordf, &bodyf, e_code);
  1234.         }
  1235.       else
  1236.         {
  1237.           /* Troff mode requires that strings be processed specially.  */
  1238.           if (troff && (*token == '"' || *token == '\''))
  1239.         {
  1240.           char qchar;
  1241.  
  1242.           qchar = *token;
  1243.           *e_code++ = '`';
  1244.           if (qchar == '"')
  1245.             *e_code++ = '`';
  1246.           e_code = chfont (&bodyf, &stringf, e_code);
  1247.  
  1248.           t_ptr = token + 1;
  1249.           while (t_ptr < token_end)
  1250.             {
  1251.               *e_code = *t_ptr++;
  1252.               if (*e_code == '\\')
  1253.             {
  1254.               *++e_code = '\\';
  1255.               if (*t_ptr == '\\')
  1256.                 *++e_code = '\\';
  1257.               /* Copy char after backslash.  */
  1258.               *++e_code = *t_ptr++;
  1259.               /* Point after the last char we copied.  */
  1260.               e_code++;
  1261.             }
  1262.             }
  1263.           e_code = chfont (&stringf, &bodyf, e_code - 1);
  1264.           if (qchar == '"')
  1265.             *e_code++ = '\'';
  1266.         }
  1267.           else
  1268.         for (t_ptr = token; t_ptr < token_end; ++t_ptr)
  1269.           {
  1270.             CHECK_CODE_SIZE;
  1271.             *e_code++ = *t_ptr;
  1272.           }
  1273.         }
  1274.       parser_state_tos->want_blank = true;
  1275.  
  1276.       /* If the token is va_dcl, it appears without a semicolon, so we
  1277.          need to pretend that one was there.  */
  1278.       if ((token_end - token) == 6
  1279.           && strncmp (token, "va_dcl", 6) == 0)
  1280.         {
  1281.           parser_state_tos->in_or_st = false;
  1282.           parser_state_tos->just_saw_decl--;
  1283.           parser_state_tos->in_decl = 0;
  1284.           parse (semicolon);
  1285.           force_nl = true;
  1286.         }
  1287.       break;
  1288.  
  1289.     case period:        /* treat a period kind of like a binary
  1290.                    operation */
  1291.       *e_code++ = '.';    /* move the period into line */
  1292.       parser_state_tos->want_blank = false;    /* dont put a blank after a
  1293.                            period */
  1294.       break;
  1295.  
  1296.     case comma:
  1297.       /* only put blank after comma if comma does not start the line */
  1298.       parser_state_tos->want_blank = (s_code != e_code);
  1299.       if (parser_state_tos->paren_depth == 0
  1300.           && parser_state_tos->in_decl
  1301.           && is_procname == 0
  1302.           && !parser_state_tos->block_init)
  1303.         while ((e_code - s_code) < (dec_ind - 1))
  1304.           {
  1305.         CHECK_CODE_SIZE;
  1306.         *e_code++ = ' ';
  1307.           }
  1308.  
  1309.       *e_code++ = ',';
  1310.       if (parser_state_tos->p_l_follow == 0)
  1311.         {
  1312.           if (parser_state_tos->block_init_level <= 0)
  1313.         parser_state_tos->block_init = 0;
  1314.           /* If we are in a declaration, and either the user wants all
  1315.              comma'd declarations broken, or the line is getting too
  1316.              long, break the line.  */
  1317.           if (break_comma &&
  1318.           (!leave_comma
  1319.            || (compute_code_target () + (e_code - s_code)
  1320.                > max_col - tabsize)))
  1321.         force_nl = true;
  1322.         }
  1323.       break;
  1324.  
  1325.     case preesc:        /* got the character '#' */
  1326.       if ((s_com != e_com) ||
  1327.           (s_lab != e_lab) ||
  1328.           (s_code != e_code))
  1329.         dump_line ();
  1330.       {
  1331.         int in_comment = 0;
  1332.         int in_cplus_comment = 0;
  1333.         int com_start = 0;
  1334.         char quote = 0;
  1335.         int com_end = 0;
  1336.  
  1337.         /* ANSI allows spaces between '#' and preprocessor directives.
  1338.            Remove such spaces unless user has specified "-lps", in
  1339.            which case also leave any space preceeding the '#'. */
  1340.         if (leave_preproc_space)
  1341.           {
  1342.         char *p = cur_line;
  1343.         while (p < buf_ptr)
  1344.           *e_lab++ = *p++;
  1345.  
  1346.         while (*buf_ptr == ' ' || *buf_ptr == TAB)
  1347.           {
  1348.             *e_lab++ = *buf_ptr++;
  1349.             if (buf_ptr >= buf_end)
  1350.               fill_buffer ();
  1351.           }
  1352.           }
  1353.         else
  1354.           {
  1355.         *e_lab++ = '#';
  1356.         while (*buf_ptr == ' ' || *buf_ptr == TAB)
  1357.           if (++buf_ptr >= buf_end)
  1358.             fill_buffer ();
  1359.           }
  1360.  
  1361.         while (*buf_ptr != EOL || (in_comment && !had_eof))
  1362.           {
  1363.         CHECK_LAB_SIZE;
  1364.         *e_lab = *buf_ptr++;
  1365.         if (buf_ptr >= buf_end)
  1366.           fill_buffer ();
  1367.         switch (*e_lab++)
  1368.           {
  1369.           case BACKSLASH:
  1370.             if (troff)
  1371.               *e_lab++ = BACKSLASH;
  1372.             if (!in_comment && !in_cplus_comment)
  1373.               {
  1374.             *e_lab++ = *buf_ptr++;
  1375.             if (buf_ptr >= buf_end)
  1376.               fill_buffer ();
  1377.               }
  1378.             break;
  1379.           case '/':
  1380.             if ((*buf_ptr == '*' || *buf_ptr == '/')
  1381.             && !in_comment && !in_cplus_comment && !quote)
  1382.               {
  1383.             if (*buf_ptr == '/')
  1384.               in_cplus_comment = 1;
  1385.             else
  1386.               in_comment = 1;
  1387.             *e_lab++ = *buf_ptr++;
  1388.             com_start = e_lab - s_lab - 2;
  1389.               }
  1390.             break;
  1391.  
  1392.           case '"':
  1393.           case '\'':
  1394.             if (!quote)
  1395.               quote = e_lab[-1];
  1396.             else
  1397.               if (e_lab[-1] == quote)
  1398.             quote = 0;
  1399.             break;
  1400.  
  1401.           case '*':
  1402.             if (*buf_ptr == '/' && in_comment)
  1403.               {
  1404.             in_comment = 0;
  1405.             *e_lab++ = *buf_ptr++;
  1406.             com_end = e_lab - s_lab;
  1407.               }
  1408.             break;
  1409.           }
  1410.           }
  1411.  
  1412.         while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == TAB))
  1413.           e_lab--;
  1414.  
  1415.         if (in_cplus_comment)
  1416.           {
  1417.         in_cplus_comment = 0;
  1418.         *e_lab++ = *buf_ptr++;
  1419.         com_end = e_lab - s_lab;
  1420.           }
  1421.  
  1422.         if (e_lab - s_lab == com_end && bp_save == 0)
  1423.           {            /* comment on preprocessor line */
  1424.         if (save_com.end != save_com.ptr)
  1425.           {
  1426.             need_chars (save_com, 2);
  1427.             *save_com.end++ = EOL;    /* add newline between
  1428.                            comments */
  1429.             *save_com.end++ = ' ';
  1430.             --line_no;
  1431.           }
  1432.         need_chars (save_com, com_end - com_start);
  1433.         strncpy (save_com.end, s_lab + com_start,
  1434.              com_end - com_start);
  1435.         save_com.end += com_end - com_start;
  1436.  
  1437.         e_lab = s_lab + com_start;
  1438.         while (e_lab > s_lab
  1439.                && (e_lab[-1] == ' ' || e_lab[-1] == TAB))
  1440.           e_lab--;
  1441.         bp_save = buf_ptr;    /* save current input buffer */
  1442.         be_save = buf_end;
  1443.         buf_ptr = save_com.ptr;    /* fix so that subsequent calls to
  1444.                        lexi will take tokens out of
  1445.                        save_com */
  1446.         need_chars (save_com, 1);
  1447.         *save_com.end++ = ' ';    /* add trailing blank, just in case */
  1448.         buf_end = save_com.end;
  1449.         save_com.end = save_com.ptr;    /* make save_com empty */
  1450.           }
  1451.         *e_lab = '\0';    /* null terminate line */
  1452.         parser_state_tos->pcase = false;
  1453.       }
  1454.  
  1455.       if (strncmp (s_lab + 1, "if", 2) == 0)
  1456.         {
  1457.           if (blanklines_around_conditional_compilation)
  1458.         {
  1459.           register c;
  1460.           prefix_blankline_requested++;
  1461.           while ((c = *in_prog_pos++) == EOL);
  1462.           in_prog_pos--;
  1463.         }
  1464.           {
  1465.         /* Push a copy of the parser_state onto the stack. All
  1466.            manipulations will use the copy at the top of stack, and
  1467.            then we can return to the previous state by popping the
  1468.            stack.  */
  1469.         struct parser_state *new;
  1470.  
  1471.         new = (struct parser_state *)
  1472.           xmalloc (sizeof (struct parser_state));
  1473.         memcpy (new, parser_state_tos, sizeof (struct parser_state));
  1474.  
  1475.         /* We need to copy the dynamically allocated arrays in the
  1476.            struct parser_state too.  */
  1477.         new->p_stack = (enum codes *)
  1478.           xmalloc (parser_state_tos->p_stack_size
  1479.                * sizeof (enum codes));
  1480.         memcpy (new->p_stack, parser_state_tos->p_stack,
  1481.               parser_state_tos->p_stack_size * sizeof (enum codes));
  1482.  
  1483.         new->il = (int *)
  1484.           xmalloc (parser_state_tos->p_stack_size * sizeof (int));
  1485.         memcpy (new->il, parser_state_tos->il,
  1486.             parser_state_tos->p_stack_size * sizeof (int));
  1487.  
  1488.         new->cstk = (int *)
  1489.           xmalloc (parser_state_tos->p_stack_size
  1490.                * sizeof (int));
  1491.         memcpy (new->cstk, parser_state_tos->cstk,
  1492.             parser_state_tos->p_stack_size * sizeof (int));
  1493.  
  1494.         new->paren_indents = (short *) xmalloc
  1495.           (parser_state_tos->paren_indents_size * sizeof (short));
  1496.         memcpy (new->paren_indents, parser_state_tos->paren_indents,
  1497.              parser_state_tos->paren_indents_size * sizeof (short));
  1498.  
  1499.         new->next = parser_state_tos;
  1500.         parser_state_tos = new;
  1501.           }
  1502.         }
  1503.       else if (strncmp (s_lab + 1, "else", 4) == 0)
  1504.         {
  1505.           /* When we get #else, we want to restore the parser state to
  1506.              what it was before the matching #if, so that things get
  1507.              lined up with the code before the #if.  However, we do not
  1508.              want to pop the stack; we just want to copy the second to
  1509.              top elt of the stack because when we encounter the #endif,
  1510.              it will pop the stack.  */
  1511.           else_or_endif = true;
  1512.           if (parser_state_tos->next)
  1513.         {
  1514.           /* First save the addresses of the arrays for the top of
  1515.              stack.  */
  1516.           enum codes *tos_p_stack = parser_state_tos->p_stack;
  1517.           int *tos_il = parser_state_tos->il;
  1518.           int *tos_cstk = parser_state_tos->cstk;
  1519.           short *tos_paren_indents =
  1520.           parser_state_tos->paren_indents;
  1521.           struct parser_state *second =
  1522.           parser_state_tos->next;
  1523.  
  1524.           memcpy (parser_state_tos, second,
  1525.               sizeof (struct parser_state));
  1526.           parser_state_tos->next = second;
  1527.  
  1528.           /* Now copy the arrays from the second to top of stack to
  1529.              the top of stack.  */
  1530.           /* Since the p_stack, etc. arrays only grow, never shrink,
  1531.              we know that they will be big enough to fit the array
  1532.              from the second to top of stack.  */
  1533.           parser_state_tos->p_stack = tos_p_stack;
  1534.           memcpy (parser_state_tos->p_stack,
  1535.               parser_state_tos->next->p_stack,
  1536.               parser_state_tos->p_stack_size
  1537.               * sizeof (enum codes));
  1538.  
  1539.           parser_state_tos->il = tos_il;
  1540.           memcpy (parser_state_tos->il,
  1541.               parser_state_tos->next->il,
  1542.               parser_state_tos->p_stack_size * sizeof (int));
  1543.  
  1544.           parser_state_tos->cstk = tos_cstk;
  1545.           memcpy (parser_state_tos->cstk,
  1546.               parser_state_tos->next->cstk,
  1547.               parser_state_tos->p_stack_size * sizeof (int));
  1548.  
  1549.           parser_state_tos->paren_indents = tos_paren_indents;
  1550.           memcpy (parser_state_tos->paren_indents,
  1551.               parser_state_tos->next->paren_indents,
  1552.               parser_state_tos->paren_indents_size
  1553.               * sizeof (short));
  1554.         }
  1555.           else
  1556.         diag (1, "Unmatched #else", 0, 0);
  1557.         }
  1558.       else if (strncmp (s_lab + 1, "endif", 5) == 0)
  1559.         {
  1560.           else_or_endif = true;
  1561.           /* We want to remove the second to top elt on the stack, which
  1562.              was put there by #if and was used to restore the stack at
  1563.              the #else (if there was one). We want to leave the top of
  1564.              stack unmolested so that the state which we have been using
  1565.              is unchanged.  */
  1566.           if (parser_state_tos->next)
  1567.         {
  1568.           struct parser_state *second = parser_state_tos->next;
  1569.  
  1570.           parser_state_tos->next = second->next;
  1571.           free (second->p_stack);
  1572.           free (second->il);
  1573.           free (second->cstk);
  1574.           free (second->paren_indents);
  1575.           free (second);
  1576.         }
  1577.           else
  1578.         diag (1, "Unmatched #endif", 0, 0);
  1579.           if (blanklines_around_conditional_compilation)
  1580.         {
  1581.           postfix_blankline_requested++;
  1582.           n_real_blanklines = 0;
  1583.         }
  1584.         }
  1585.  
  1586.       /* Normally, subsequent processing of the newline character
  1587.          causes the line to be printed.  The following clause handles
  1588.          a special case (comma-separated declarations separated
  1589.          by the preprocessor lines) where this doesn't happen. */
  1590.       if (parser_state_tos->last_token == comma
  1591.           && parser_state_tos->p_l_follow <= 0
  1592.           && leave_comma && !parser_state_tos->block_init
  1593.           && break_comma && s_com == e_com)
  1594.         {
  1595.           dump_line ();
  1596.           parser_state_tos->want_blank = false;
  1597.         }
  1598.       break;
  1599.  
  1600.       /* A C or C++ comment. */
  1601.     case comment:
  1602.     case cplus_comment:
  1603.       if (flushed_nl)
  1604.         {
  1605.           flushed_nl = false;
  1606.           dump_line ();
  1607.           parser_state_tos->want_blank = false;
  1608.           force_nl = false;
  1609.         }
  1610.       print_comment ();
  1611.       break;
  1612.     }            /* end of big switch stmt */
  1613.  
  1614.       *e_code = '\0';        /* make sure code section is null terminated */
  1615.       if (type_code != comment
  1616.       && type_code != cplus_comment
  1617.       && type_code != newline
  1618.       && type_code != preesc
  1619.       && type_code != form_feed)
  1620.     parser_state_tos->last_token = type_code;
  1621.  
  1622.     }                /* end of main while (1) loop */
  1623. }
  1624.  
  1625.  
  1626.  
  1627. char *set_profile ();
  1628. void set_defaults ();
  1629. int set_option ();
  1630.  
  1631. /* Points to current input file */
  1632. char *in_name = 0;
  1633.  
  1634. /* Points to the name of the output file */
  1635. char *out_name = 0;
  1636.  
  1637. /* How many input files were specified */
  1638. int input_files;
  1639.  
  1640. /* Names of all input files */
  1641. char **in_file_names;
  1642.  
  1643. /* Initial number of input filenames to allocate. */
  1644. int max_input_files = 128;
  1645.  
  1646.  
  1647. #ifdef DEBUG
  1648. int debug;
  1649. #endif
  1650.  
  1651. main (argc, argv)
  1652.      int argc;
  1653.      char **argv;
  1654. {
  1655.   register int i;
  1656.   struct file_buffer *current_input;
  1657.   char *profile_pathname = 0;
  1658.   int using_stdin = false;
  1659.  
  1660. #ifdef MPW
  1661.   InitCursorCtl (NULL);
  1662.   SpinCursor (1);
  1663. #endif
  1664.  
  1665. #ifdef DEBUG
  1666.   if (debug)
  1667.     debug_init ();
  1668. #endif
  1669.  
  1670.   init_parser ();
  1671.   initialize_backups ();
  1672.  
  1673.   output = 0;
  1674.   input_files = 0;
  1675.   in_file_names = (char **) xmalloc (max_input_files * sizeof (char *));
  1676.  
  1677.   set_defaults ();
  1678.   for (i = 1; i < argc; ++i)
  1679.     if (strcmp (argv[i], "-npro") == 0
  1680.     || strcmp (argv[i], "--ignore-profile") == 0
  1681.     || strcmp (argv[i], "+ignore-profile") == 0)
  1682.       break;
  1683.   if (i >= argc)
  1684.     profile_pathname = set_profile ();
  1685.  
  1686.   for (i = 1; i < argc; ++i)
  1687.     {
  1688.       if (argv[i][0] != '-' && argv[i][0] != '+')    /* Filename */
  1689.     {
  1690.       if (expect_output_file == true)    /* Last arg was "-o" */
  1691.         {
  1692.           if (out_name != 0)
  1693.         {
  1694.           fprintf (stderr, "indent: only one output file (2nd was %s)\n", argv[i]);
  1695.           exit (1);
  1696.         }
  1697.  
  1698.           if (input_files > 1)
  1699.         {
  1700.           fprintf (stderr, "indent: only one input file when output file is specified\n");
  1701.           exit (1);
  1702.         }
  1703.  
  1704.           out_name = argv[i];
  1705.           expect_output_file = false;
  1706.           continue;
  1707.         }
  1708.       else
  1709.         {
  1710.           if (using_stdin)
  1711.         {
  1712.           fprintf (stderr, "indent: can't have filenames when specifying standard input\n");
  1713.           exit (1);
  1714.         }
  1715.  
  1716.           input_files++;
  1717.           if (input_files > 1)
  1718.         {
  1719.           if (out_name != 0)
  1720.             {
  1721.               fprintf (stderr, "indent: only one input file when output file is specified\n");
  1722.               exit (1);
  1723.             }
  1724.  
  1725.           if (use_stdout != 0)
  1726.             {
  1727.               fprintf (stderr, "indent: only one input file when stdout is used\n");
  1728.               exit (1);
  1729.             }
  1730.  
  1731.           if (input_files > max_input_files)
  1732.             {
  1733.               max_input_files = 2 * max_input_files;
  1734.               in_file_names
  1735.             = (char **) xrealloc ((char *) in_file_names,
  1736.                           (max_input_files
  1737.                            * sizeof (char *)));
  1738.             }
  1739.         }
  1740.  
  1741.           in_file_names[input_files - 1] = argv[i];
  1742.         }
  1743.     }
  1744.       else
  1745.     {
  1746.       /* '-' as filename means stdin. */
  1747.       if (argv[i][0] == '-' && argv[i][1] == '\0')
  1748.         {
  1749.           if (input_files > 0)
  1750.         {
  1751.           fprintf (stderr, "indent: can't have filenames when specifying standard input\n");
  1752.           exit (1);
  1753.         }
  1754.  
  1755.           using_stdin = true;
  1756.         }
  1757.       else
  1758.         i += set_option (argv[i], (i < argc ? argv[i + 1] : 0), 1);
  1759.     }
  1760.     }
  1761.  
  1762.   if (verbose && profile_pathname)
  1763.     fprintf (stderr, "Read profile %s\n", profile_pathname);
  1764.  
  1765.   if (input_files > 1)
  1766.     {
  1767.       /* When multiple input files are specified, make a backup copy
  1768.      and then output the indented code into the same filename. */
  1769.  
  1770.       for (i = 0; input_files; i++, input_files--)
  1771.     {
  1772.       current_input = read_file (in_file_names[i]);
  1773.       in_name = out_name = in_file_names[i];
  1774.       output = fopen (out_name, "w");
  1775.       if (output == 0)
  1776.         {
  1777.           fprintf (stderr, "indent: can't create %s\n", out_name);
  1778.           exit (1);
  1779.         }
  1780.  
  1781. #ifdef MPW
  1782.       fsetfileinfo (out_name, 'MPS ', 'TEXT');
  1783.       SpinCursor (1);
  1784. #endif
  1785.       make_backup (current_input);
  1786.       reset_parser ();
  1787. #ifdef MPW
  1788.       SpinCursor (1);
  1789. #endif
  1790.       indent (current_input);
  1791.       if (fclose (output) != 0)
  1792.         sys_error (out_name);
  1793.     }
  1794.     }
  1795.   else
  1796.     {
  1797.       /* One input stream -- specified file, or stdin */
  1798.  
  1799.       if (input_files == 0 || using_stdin)
  1800.     {
  1801.       input_files = 1;
  1802.       in_file_names[0] = "Standard input";
  1803.       current_input = read_stdin ();
  1804.     }
  1805.       else
  1806.     /* 1 input file */
  1807.     {
  1808.       current_input = read_file (in_file_names[0]);
  1809.       if (!out_name && !use_stdout)
  1810.         {
  1811.           out_name = in_file_names[0];
  1812.           make_backup (current_input);
  1813.         }
  1814.     }
  1815.       in_name = in_file_names[0];
  1816.  
  1817.       /* Uset stdout if it was specified ("-st"), or neither input
  1818.          nor output file was specified, or we're doing troff. */
  1819.       if (use_stdout || !out_name || troff)
  1820.     output = stdout;
  1821.       else
  1822.     {
  1823.       output = fopen (out_name, "w");
  1824.       if (output == 0)
  1825.         {
  1826.           fprintf (stderr, "indent: can't create %s\n", out_name);
  1827.           exit (1);
  1828.         }
  1829. #ifdef MPW
  1830.       fsetfileinfo (out_name, 'MPS ', 'TEXT');
  1831. #endif
  1832.     }
  1833.  
  1834.       reset_parser ();
  1835.       indent (current_input);
  1836.     }
  1837.  
  1838.   exit (0);
  1839. }
  1840.